import { IHttpHelper, IRequest } from "../../ADCore/Interfaces/INetwork"; 
import HttpGloableConfig from "../../ADCore/Network/HttpGloableConfig";

export default class HttpHelper implements IHttpHelper {
  post<T>(url: string, data: any): Promise<T> {
    return this.request({
      url: url,
      data: data,
      method: "post",
      responseType: "json",
    });
  }

  get<T>(url: string, data: any): Promise<T> {
    return this.request({
      url: url,
      params: data,
      method: "get",
      responseType: "json",
    });
  }

  request<T>(options: IRequest): Promise<T> {
    var isFilter =
      HttpGloableConfig.filter != null &&
      HttpGloableConfig.filter.routeFitler(options);
    return isFilter
      ? this.requestFilter(options)
      : this.requestNoFilter(options);
  }
  /**有拦截器请求 */
  private requestFilter<T>(options: IRequest): Promise<T> {
    return new Promise((resolve, reject) => {
      try {
        //请求前参数配置拦截
        HttpGloableConfig.filter.beforeCreate(options);
        options = this.parseParams(options);
        var xhr: Laya.HttpRequest = new Laya.HttpRequest();
        xhr.http.timeout = HttpGloableConfig.timeout; //设置超时时间；
        //请求后参数拦截
        HttpGloableConfig.filter.created(options, xhr);
        xhr.once(Laya.Event.COMPLETE, this, (result) => {
          HttpGloableConfig.filter.completed(result, options);
          resolve(result);
        });
        xhr.once(Laya.Event.ERROR, this, (error) => {
          reject.call(error);
        });
        //是否监请求听进度
        if (options.progress)
          xhr.on(
            Laya.Event.PROGRESS,
            options.progress.caller,
            options.progress.method
          );
        xhr.send(
          options.url,
          options.data,
          options.method,
          options.responseType,
          options.headers
        );
      } catch (error) {
        HttpGloableConfig.filter.error(error);
        reject.call(error);
      }
    });
  }
  /**无拦截器请求 */
  private requestNoFilter<T>(options: IRequest): Promise<T> {
    return new Promise((resolve, reject) => {
      try {
        //请求前参数配置拦截
        options = this.parseParams(options);
        var xhr: Laya.HttpRequest = new Laya.HttpRequest();
        xhr.http.timeout = HttpGloableConfig.timeout; //设置超时时间；
        xhr.once(Laya.Event.COMPLETE, this, (result) => {
          resolve(result);
        });
        xhr.once(Laya.Event.ERROR, this, (error) => {
          reject(error);
        });
        //是否监请求听进度
        if (options.progress)
          xhr.on(
            Laya.Event.PROGRESS,
            options.progress.caller,
            options.progress.method
          );
        xhr.send(
          options.url,
          options.data,
          options.method,
          options.responseType,
          options.headers
        );
      } catch (error) {
        reject.call(error);
      }
    });
  }
  /**参数解析 */
  private parseParams(options: IRequest): IRequest {
    if (options.params && (!options.method || options.method == "get")) {
      options.url = "?" + options.params;
    }
    if (options.url.indexOf("http") < 0) {
      options.url = HttpGloableConfig.baseUrl + options.url;
    }
    if (options.data) {
      options.data = JSON.stringify(options.data);
    }

    options.headers = HttpGloableConfig.getHeaders(
      options.method,
      options.headers
    );
    return options;
  }
}
